package com.qf.service.impl;

import com.qf.dao.OrderDao;
import com.qf.dao.impl.OrderDaoImpl;
import com.qf.domain.*;
import com.qf.service.*;
import com.qf.utils.DataSourceUtil;

import java.sql.SQLException;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao=new OrderDaoImpl();

    @Override
    public void submitOrder(Order order, List<Cart> cartList) {
        try {
            DataSourceUtil.begin();
            orderDao.addOrder(order);
            for (Cart cart : cartList) {
                OrderDetail orderDetail=new OrderDetail(0,order.getId(),cart.getPid(),cart.getNum(),cart.getMoney());
                orderDao.addOrderDetail(orderDetail);

            }
            CartService cartService=new CartServiceImpl();
            cartService.clearAll(order.getUid());

            DataSourceUtil.commit();

        } catch (Exception e) {
            try {
                DataSourceUtil.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
           throw new RuntimeException(e);
        } finally {
            try {
                DataSourceUtil.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Order> selectByUserId(Integer uid) {
        List<Order>orderList=orderDao.findByUserId(uid);
       if(orderList!=null&& orderList.size()>0){
           AddressService addressService=new AddressServiceImpl();
           List<Address> addressList = addressService.selectByUserId(uid);
           if(addressList!=null){
               for (Order order : orderList) {
                   for (Address address : addressList) {
                       if(order.getAid()==address.getId()){
                           order.setAddress(address);
                       }
                   }
               }
           }
       }
        return orderList;
    }

    @Override
    public Order selectByOid(String oid) {
        Order order = orderDao.findByOid(oid);
        AddressService addressService=new AddressServiceImpl();
        List<Address> addressList = addressService.selectByUserId(order.getUid());
        if (addressList!=null && addressList.size()>0) {
            for (Address address : addressList) {
                if(order.getAid()==address.getId()){
                    order.setAddress(address);
                }
            }
        }
        return order;
    }

    @Override
    public List<OrderDetail> selectOrderDetail(String oid) {
        List<OrderDetail> orderDetails=orderDao.findDetail(oid);
        if(orderDetails!=null &&orderDetails.size()>0){
            for (OrderDetail orderDetail : orderDetails) {
                GoodsService goodsService =new GoodsServiceImpl();
                Goods goods = goodsService.selectById(orderDetail.getPid());
                orderDetail.setGoods(goods);
            }
        }
        return orderDetails;
    }

    @Override
    public List<Order> selectAll(String where, List<Object> params) {
        List<Order> orderList=orderDao.findAll(where,params);
        if(orderList!=null&&orderList.size()>0){
            for (Order order : orderList) {
                UserService userService=new UserServiceImpl();
                User user = userService.selectByUId(order.getUid());
                order.setUser(user);
            }
        }
        return orderList;
    }

    @Override
    public void deleteByOId(String oid) {
        orderDao.removeByOId(oid);
    }

    @Override
    public void updateStatusById3(String oid) {
        orderDao.modifyStatusById3(oid);
    }

    @Override
    public void updateStatusById2(String oid) {
        orderDao.modifyStatusById2(oid);
    }

    @Override
    public void updateStatusById4(String oid) {
        orderDao.modifyStatusById4(oid);
    }
}
